package com.cpp.server.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.cpp.common.constant.ModuleConstant;
import com.cpp.common.result.PageResult;
import com.cpp.common.result.Result;
import com.cpp.pojo.dto.resource.ResourcePageDTO;
import com.cpp.pojo.vo.question.QuestionVO;
import com.cpp.pojo.vo.resource.ResourceVO;
import com.cpp.server.aspect.annotation.MyLog;
import com.cpp.server.service.ResourceService;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
//课件资源管理：按学科列表教师备课产生的课件、练习等资源，可以导出。

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/resource")
@MyLog(module = ModuleConstant.OTHERS)
public class ResourceController {
    private final ResourceService resourceService;
//    生成对应章节的ppt
    @GetMapping(value = "/generatePpt")
    @MyLog(module = ModuleConstant.PRE_CLASS)
    public Result generateChapterPpt(@RequestParam Long chapterId){
        String pptUrl =  resourceService.generatePpt(chapterId);
        return Result.success(pptUrl);
    }
    //生成某个已结束考试的总结ppt
    @GetMapping(value = "/generateExamReviewPpt")
    @MyLog(module = ModuleConstant.PRE_CLASS)
    public Result generateExamReviewPpt(@RequestParam Long examId){
        String pptUrl =  resourceService.generateExamReviewPpt(examId);
        return Result.success(pptUrl);
    }
    //课件资源管理：按学科列表教师备课产生的课件,可以,下载
    @GetMapping(value = "/listPpt")
    public Result listResourceBySubject( ResourcePageDTO resourcePageDTO){
        PageResult resourceVOs =  resourceService.listResourceBySubject(resourcePageDTO);
        return Result.success(resourceVOs);
    }

    //按学科列表教师备课产生的练习资源，
    @GetMapping(value = "/listQuestion")
    public Result<PageResult> listQuestion (ResourcePageDTO resourcePageDTO){
        PageResult pageQuestionVOS = resourceService.listQuestion(resourcePageDTO);
        return Result.success(pageQuestionVOS);
    }

    //按学科导出excel教师备课产生的课件
    @SneakyThrows
    @GetMapping(value = "/exportPptExcel")
    public void exportPptExcel(Long subjectId,HttpServletResponse response){
        //查询列表
        PageResult resourceVOs =  resourceService.listResourceBySubject(new ResourcePageDTO(1,100, subjectId));
        log.info("查询列表：{}",resourceVOs);
        //导出为ppt
        String fileName ="课件excel";
        this.setExcelResponseProp(response, fileName);
        EasyExcel.write(response.getOutputStream())
                .head(ResourceVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(fileName)
                .doWrite(resourceVOs.getRecords());
//        return Result.success();
    }


    //按学科导出excel教师备课产生的练习资源，
    @SneakyThrows
    @GetMapping(value = "/exportQuestion")
    public void exportQuestion ( Long subjectId, HttpServletResponse response){
        PageResult questionVOS = resourceService.listQuestion(new ResourcePageDTO(1,200,subjectId));
        String fileName ="练习excel";
        log.info("查询列表：{}",questionVOS);
        this.setExcelResponseProp(response, fileName);
        EasyExcel.write(response.getOutputStream())
                .head(QuestionVO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(fileName)
                .doWrite(questionVOS.getRecords());
//        return Result.success();
    }
    /**
     * 设置响应结果
     *
     * @param response    响应结果对象
     * @param rawFileName 文件名
     * @throws UnsupportedEncodingException 不支持编码异常
     */
    private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }
}
